// stores/auth.ts
import { defineStore } from 'pinia';
interface AuthState {
  token: string | null;
  user: any | null;
  _id: string | null;
}

export const useAuthStore = defineStore('auth', {
  state: (): AuthState => ({
    token:  null,
    user:  null,
    _id: null
  }),
  actions: {
    setToken(token: string) {
        this.token = token;
        if (typeof window !== 'undefined') {
          localStorage.setItem('authToken', token);
        }
    },
    setId(id: string) {
      this._id = id;
      if (typeof window !== 'undefined') {
        localStorage.setItem('_id', id)
      }
  },
    setUser(user: any) {
        this.user = user;
        if (typeof window !== 'undefined') {
          localStorage.setItem('authUser', user)
        }
    },
    logout() {
        this.$reset();
        if (typeof window !== 'undefined') {
          localStorage.clear()
        }
    },
  },
});